package com.tothefor.controller;

import com.tothefor.pojo.entity.QuestionConnect;
import com.tothefor.pojo.entity.Questionnaire;
import com.tothefor.pojo.entity.QuestionnaireResult;
import com.tothefor.pojo.entity.QuestionnaireResultCount;
import com.tothefor.resultR.R;
import com.tothefor.service.impl.QuestionConnectServiceImpl;
import com.tothefor.service.impl.QuestionnaireResultCountServiceImpl;
import com.tothefor.service.impl.QuestionnaireResultServiceImpl;
import com.tothefor.service.impl.QuestionnaireServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/questionnaireResultCount")
public class QuestionnaireResultCountController {
    @Autowired
    private QuestionnaireResultCountServiceImpl QuestionnaireResultCountService;

    @Autowired
    private QuestionnaireResultServiceImpl QuestionnaireResultService;

    @Autowired
    private QuestionConnectServiceImpl QuestionConnectServiceImpl;

    @Autowired
    private QuestionnaireServiceImpl QuestionnaireServiceImpl;

    @PostMapping("/setResultcount")
    public R<?> Setresultcount(){
        List<QuestionnaireResultCount> qsc = QuestionnaireResultCountService.list();
        if(qsc.size()!=0){
            for(QuestionnaireResultCount q:qsc){
                QuestionnaireResultCountService.removeById(q.getId());
            }
        }

        List<QuestionnaireResult> qs = QuestionnaireResultService.list();
        List<String> questionID = new ArrayList<>();
        for(QuestionnaireResult q:qs){
            if(q.getAnswertext()==null){
                if(questionID.size()==0){
                    questionID.add(q.getQuestionId());
                }else{
                    int i;
                    int sum = questionID.size();
                    for(i=0;i<sum;i++){
                        if (questionID.get(i).equals(q.getQuestionId())){
                        }else{
                            if(!questionID.contains(q.getQuestionId())){
                                questionID.add(q.getQuestionId());
                            }
                        }
                    }
                }
            }

        }
        for(String s:questionID){
            QuestionnaireResultCount qc = new QuestionnaireResultCount();
            int countA=0;
            int countB=0;
            int countC=0;
            int countD=0;
            for(QuestionnaireResult q:qs){
                if(s.equals(q.getQuestionId())){

                        if(q.getAnswer1()==1){
                            countA++;
                        }if(q.getAnswer2()==1){
                            countB++;
                        }if(q.getAnswer3()==1){
                            countC++;
                        }if(q.getAnswer4()==1){
                            countD++;
                        }

                }
            }
            qc.setQuestionId(s);
            qc.setCountA(countA);
            qc.setCountB(countB);
            qc.setCountC(countC);
            qc.setCountD(countD);
            List<QuestionConnect> questions = QuestionConnectServiceImpl.list();
            for(QuestionConnect q:questions){
                if(String.valueOf(q.getQuestionId()).equals(s)){
                    qc.setQuestionnaireId(String.valueOf(q.getNaireId()));
                }
            }
            QuestionnaireResultCountService.save(qc);
        }
        return R.SUCCESS();
    }

    @PostMapping("/showQuestionnaireCount")
    public R<?> showQuestionnaireCount(@RequestBody QuestionnaireResultCount q){
        List<QuestionnaireResultCount> qs = QuestionnaireResultCountService.list();
        List<QuestionnaireResultCount> needCounts= new ArrayList<>();
        for(QuestionnaireResultCount qc:qs){
            if(qc.getQuestionnaireId().equals(q.getQuestionnaireId())){
                needCounts.add(qc);
            }
        }
        return R.SUCCESS(needCounts);
    }
    @PostMapping("/showAllCount")
    public R<?> showAllCount(@RequestBody Questionnaire q){
        List<Questionnaire> qn = QuestionnaireServiceImpl.list();
        List<QuestionnaireResultCount> qc = QuestionnaireResultCountService.list();
        List<QuestionnaireResultCount> needCount = new ArrayList<>();
        for(QuestionnaireResultCount qrc:qc){
            for(Questionnaire qnaire:qn){
                if(q.getUsername().equals(qnaire.getUsername())){
                    if(!needCount.contains(qrc)){
                        needCount.add(qrc);
                    }
                }
            }
        }
        return R.SUCCESS(needCount);
    }
}
